package persistency.reader;

/**
 * OfferedCourseReader.java
 * Purpose: this class represents a course that students can take
 * 
 * @author Hans Brems
 * @version 1.0 05/11/2011
 */

import java.util.Set;
import java.util.TreeSet;

import model.Registration;
import model.Student;
import model.container.StudentContainer;
import util.Date;

public class RegistrationReader extends ContentReader {
	
	/** Constructs a registration reader object */
	public RegistrationReader() {
	}
	
	/** Constructs a registration reader object */
	public RegistrationReader(String filePath) {
		super(filePath);
	}
	
	/** Reads a text file containing registration objects into a collection */
	public Set<Registration> read() {
		
		StudentContainer studentContainer = StudentContainer.getInstance();
		
		// Create list
		Set<Registration> registrations = new TreeSet<Registration>();
		
		
		// Skip the first line (header)
		this.scanner.nextLine();

		// Construct an offered course object from each line
		while (this.scanner.hasNext()) {
			
			
			
			// Create a new offered course
			Registration registration = new Registration();
			registration.setPrimaryKey(Integer.parseInt(this.scanner.next()));
			Student student = studentContainer.getStudentByID(this.scanner.next());
			student.getRegistrations().add(registration);
			registration.setStudent(student);
			
			try {
				registration.setRegistrationDate(new Date(this.scanner.next()));

			} catch (Exception e) {
				e.printStackTrace();
			}
			
			// Add course to collection
			registrations.add(registration);
		}
		
		// Return list
		return registrations;
	}
}